#include "Manager.h"

//默认构造
Manager::Manager()
{
    
}

//有参构造
Manager::Manager(string name, string password)
{
    //初始化管理员信息
    this->name = name;
    this->password = password;

    //初始化容器 获取文件中 老师、学生信息
    initVector();

    //初始化机房信息
    ifstream ifs;
    ifs.open(COMPUTER_FILE, ios::in);

    ComputerRoom com;
    while (ifs >> com.comId && ifs >> com.maxNum)
    {
        vCom.push_back(com);
    }
    ifs.close();
    
    cout << "机房的数量为：" << vCom.size() << endl;
}

//菜单界面
void Manager::openMenu()
{
    cout << "欢迎管理员：" << this->name << " 登录！" << endl;
    cout << "\t\t------------------------" << endl;
    cout << "\t\t|                      |" << endl;
    cout << "\t\t|     1、添加账号      |" << endl;
    cout << "\t\t|                      |" << endl;
    cout << "\t\t|     2、查看账号      |" << endl;
    cout << "\t\t|                      |" << endl;
    cout << "\t\t|     3、查看机房      |" << endl;
    cout << "\t\t|                      |" << endl;
    cout << "\t\t|     4、清空预约      |" << endl;
    cout << "\t\t|                      |" << endl;
    cout << "\t\t|     0、注销登录      |" << endl;
    cout << "\t\t|                      |" << endl;
    cout << "\t\t------------------------" << endl;
}

//添加账号
void Manager::addPerson()
{
    cout << "请输入添加账号类型" << endl;
    cout << "1、添加学生" << endl;
    cout << "2、添加老师" << endl;

    string fileName;    //操作文件名
    string tip;     //提示id号
    string errorTip;    //重复错误提示

    ofstream ofs;   //文件操作对象

    int select = 0;
    cin >> select;  //接受用户的选项

    if (select == 1)
    {
        //添加的是学生
        fileName = STUDENT_FILE;
        tip = "请输入学号：";
        errorTip = "学号重复，请重新输入";
    }
    else if (select == 2)
    {
        //添加的是教师
        fileName = TEACHER_FILE;
        tip = "请输入职工编号：";
        errorTip = "职工号重复，请重新输入";
    }

    //利用追加的方式 写文件
    ofs.open(fileName, ios::out | ios::app);

    int id; // 学号 || 职工号
    string name;    //姓名
    string password;    //密码

    cout << tip << endl;
    
    while (true)
    {
        cin >> id;
        bool ret = checkRepeat(id, select);
        if (ret) //有重复
        {
            cout << errorTip << endl;
        }
        else
        {
            break;
        }
    }

    cout << "请输入姓名：" << endl;
    cin >> name;

    cout << "请输入密码：" << endl;
    cin >> password;

    //向文件中添加数据
    ofs << id << " " << name << " " << password << endl;
    cout << "添加成功！" << endl;

    ofs.close();

    //调用初始化容器接口，重新获取文件中的数据
    initVector();

}

void printStudent(Student& s)
{
    cout << "学号：" << s.id << " 姓名："
        << s.name << " 密码：" << s.password << endl;
}

void printTeacher(Teacher& s)
{
    cout << "职工号：" << s.empId << " 姓名："
        << s.name << " 密码：" << s.password << endl;
}

//查看账号
void Manager::showPerson()
{
    cout << "请选择查看内容：" << endl;
    cout << "1、查看所有学生" << endl;
    cout << "2、查看所有老师" << endl;

    int select = 0;  //接受用户选择
    cin >> select;

    if (select == 1)
    {
        //查看学生
        cout << "所有学生信息如下：" << endl;
        for_each(vStu.begin(), vStu.end(), printStudent);
    }
    else
    {
        //查看老师
        cout << "所有老师信息如下：" << endl;
        for_each(vTea.begin(), vTea.end(), printTeacher);
    }
}

//查看机房信息
void Manager::showComputer()
{
    cout << "机房信息如下：" << endl;

    for (vector<ComputerRoom>::iterator it = vCom.begin();it != vCom.end();it++)
    {
        cout << "机房编号：" << it->comId
            << " 机房最大容量：" << it->maxNum << endl;
    }
}

//清空预约记录
void Manager::cleanFile()
{
    ofstream ofs(ORDER_FILE, ios::trunc);
    ofs.close();

    cout << "清空成功！" << endl;
    
}

//初始化容器
void Manager::initVector()
{
    //确保容器清空状态
    vStu.clear();
    vTea.clear();

    //读取信息      学生
    ifstream ifs;
    ifs.open(STUDENT_FILE, ios::in);

    if (!ifs.is_open())
    {
        cout << "文件读取失败" << endl;
        return;
    }

    Student s;
    while (ifs >> s.id && ifs >> s.name && ifs >> s.password)
    {
        vStu.push_back(s);
    }

    cout << "当前学生数量为：" << vStu.size() << endl;
    ifs.close();

    //读取信息  老师
    ifs.open(TEACHER_FILE, ios::in);
    Teacher t;
    while (ifs >> t.empId && ifs >> t.name && ifs >> t.password)
    {
        vTea.push_back(t);
    }

    cout << "当前老师数量为：" << vTea.size() << endl;
    ifs.close();
}

//检测重复      参数1 检测学号/职工号   参数2 检测类型
bool Manager::checkRepeat(int id, int type)
{
    if (type == 1)
    {
        //检测学生
        for (vector<Student>::iterator it = vStu.begin();it != vStu.end();it++)
        {
            if (id == it->id)
            {
                return true;
            }
        }
    }
    else
    {
        //检测老师
        for (vector<Teacher>::iterator it = vTea.begin();it != vTea.end();it++)
        {
            if (id == it->empId)
            {
                return true;
            }
        }
    }

    return false;
}